// 本文件可写入c++代码








































#if 0

#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>


int testVideo()
{
  // 打开摄像头设备
  int camera_fd = open("/dev/video0", O_RDWR);
  if (camera_fd == -1) {
      std::cerr << "无法打开摄像头设备" << std::endl;
      return 1;
  }
  // 查询和设置摄像头参数
  v4l2_capability cap;
  if (ioctl(camera_fd, VIDIOC_QUERYCAP, &cap) == -1) {
      std::cerr << "无法查询摄像头能力" << std::endl;
      close(camera_fd);
      return 1;
  }
  v4l2_format fmt{};
  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  fmt.fmt.pix.width = 640;  // 设置分辨率宽度
  fmt.fmt.pix.height = 480; // 设置分辨率高度
  fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG; // 设置图像格式为JPEG
  if (ioctl(camera_fd, VIDIOC_S_FMT, &fmt) == -1) {
      std::cerr << "无法设置摄像头格式" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 请求缓冲区
  v4l2_requestbuffers req{};
  req.count = 1; // 请求一个缓冲区
  req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  req.memory = V4L2_MEMORY_MMAP;
  if (ioctl(camera_fd, VIDIOC_REQBUFS, &req) == -1) {
      std::cerr << "无法请求缓冲区" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 映射缓冲区到用户空间
  v4l2_buffer buffer{};
  buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  buffer.memory = V4L2_MEMORY_MMAP;

  if (ioctl(camera_fd, VIDIOC_QUERYBUF, &buffer) == -1) {
    std::cerr << "无法查询缓冲区信息" << std::endl;
    close(camera_fd);
    return 1;
  }

  void* frame_buffer = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, camera_fd, buffer.m.offset);
  if (frame_buffer == MAP_FAILED) {
      std::cerr << "无法映射帧数据" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 将缓冲区入队
  if (ioctl(camera_fd, VIDIOC_QBUF, &buffer) == -1) {
      std::cerr << "无法将缓冲区入队" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 启动捕捉
  int type = buffer.type;
  if (ioctl(camera_fd, VIDIOC_STREAMON, &type) == -1) {
      std::cerr << "无法启动捕捉" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 从缓冲区取出帧数据
  if (ioctl(camera_fd, VIDIOC_DQBUF, &buffer) == -1) {
      std::cerr << "无法取出帧数据" << std::endl;
      close(camera_fd);
      return 1;
  }

  // 存储帧数据到文件
  std::ofstream output_file("photo.jpg", std::ios::binary | std::ios::out);
  output_file.write(static_cast<char*>(frame_buffer), buffer.bytesused);
  output_file.close();

  // 将空闲缓冲区重新入队
  if (ioctl(camera_fd, VIDIOC_QBUF, &buffer) == -1) {
      std::cerr << "无法重新入队空闲缓冲区" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 停止捕捉
  if (ioctl(camera_fd, VIDIOC_STREAMOFF, &type) == -1) {
      std::cerr << "无法停止捕捉" << std::endl;
      close(camera_fd);
      return 1;
  }
  // 关闭摄像头设备
  close(camera_fd);
  return 0;
}
#endif